package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.Factory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Worker;
import com.ruoyi.system.service.IWorkerService;
import com.ruoyi.system.service.IFactoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工人Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@Controller
@RequestMapping("/system/worker")
public class WorkerController extends BaseController
{
    private String prefix = "system/worker";

    @Autowired
    private IWorkerService workerService;
    @Autowired
    private IFactoryService sysFactoryService;
    @RequiresPermissions("system:worker:view")
    @GetMapping()
    public String worker()
    {
        return prefix + "/worker";
    }

    /**
     * 查询工人列表
     */
    @RequiresPermissions("system:worker:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Worker worker)
    {
        startPage();
        List<Worker> list = workerService.selectWorkerList(worker);
        //TODO 实现通过工厂id查到工厂名字
        for (Worker sysWorker1 : list) {
            Long factoryId = sysWorker1.getFactoryId();
            //根据工厂的编号查询工厂名称
            Factory factory = sysFactoryService.selectFactoryByFactoryId(Long.valueOf(factoryId));
            if(factory != null) {
                String factoryName = factory.getFactoryName();
                sysWorker1.setFactoryName(factoryName);
            }
        }



        return getDataTable(list);
    }

    /**
     * 导出工人列表
     */
    @RequiresPermissions("system:worker:export")
    @Log(title = "工人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        return util.exportExcel(list, "工人数据");
    }

    /**
     * 新增工人
     */
    @RequiresPermissions("system:worker:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工人
     */
    @RequiresPermissions("system:worker:add")
    @Log(title = "工人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Worker worker)
    {
        return toAjax(workerService.insertWorker(worker));
    }

    /**
     * 修改工人
     */
    @RequiresPermissions("system:worker:edit")
    @GetMapping("/edit/{workerId}")
    public String edit(@PathVariable("workerId") Long workerId, ModelMap mmap)
    {
        Worker worker = workerService.selectWorkerByWorkerId(workerId);
        mmap.put("worker", worker);
        return prefix + "/edit";
    }

    /**
     * 修改保存工人
     */
    @RequiresPermissions("system:worker:edit")
    @Log(title = "工人", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Worker worker)
    {
        return toAjax(workerService.updateWorker(worker));
    }

    /**
     * 删除工人
     */
    @RequiresPermissions("system:worker:remove")
    @Log(title = "工人", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workerService.deleteWorkerByWorkerIds(ids));
    }
}
